/**
 * 
 */
package cardbs.servlets;

import static cardbs.dao.DAOFactory.*;
import static cardbs.dao.PartDAO.*;
import static cardbs.domain.Part.*;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cardbs.dao.DAOFactory;
import cardbs.dao.DepartmentDAO;
import cardbs.dao.EmployeeDAO;
import cardbs.dao.PartDAO;
import cardbs.dao.WorkDAO;
import cardbs.domain.Department;
import cardbs.domain.Deptype;
import cardbs.domain.Employee;
import cardbs.domain.Part;
import cardbs.domain.ServicePoint;
import cardbs.domain.UserType;
import cardbs.domain.Work;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author marianna
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class InsertPartInfoServlet extends HttpServlet{
	public InsertPartInfoServlet() {
		// TODO Auto-generated constructor stub
	}
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param request
	 * @param response
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		// begin-user-code
		// TODO Auto-generated method stub
		//parametroi apo to POST
		String departmentCode = request.getParameter("department");
		String partName;
		String company;
		String manufacturyYear;
		int employeeCode;
		Double price;
		//parametroi apo to POST
		HttpSession session = request.getSession(true);
		Employee administrator = (Employee)session.getAttribute("Administrator");
		employeeCode = administrator.getEmployeeCode();
		//depCode = department.getDepCode();
		partName = request.getParameter("partName");
		company = request.getParameter("company");
		manufacturyYear = request.getParameter("manufacturyYear");
		price = Double.parseDouble(request.getParameter("price"));
		Deptype depCode = Deptype.valueOf(Deptype.class, departmentCode);
		DAOFactory daoFactory = DAOFactory.getInstance();
		DepartmentDAO departmentDAO = daoFactory.getDepartmentDAO();

		Department department = departmentDAO.find(depCode);
		PartDAO partDAO = daoFactory.getPartDAO();
		
		Boolean part = partDAO.findPart(partName);
		
		if(part==true){
		      try {
			    	HttpSession sess = request.getSession(true);
					sess.setAttribute("errorPart", "true");
					response.sendRedirect("/car-dbs-implementation/uipages/adminpages/eisagogiAntallaktikou.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
				}
		else {			
			Part newPart = new Part();
			newPart.setPartName(partName);
			newPart.setPrice(price);
			newPart.setCompany(company);
			newPart.setManufacturyYear(manufacturyYear);
			newPart.setDepartment(department);
			//newPart.setDepartment(depCode);
			partDAO.save(newPart);
			 try {
				    HttpSession sess = request.getSession(true);
					sess.setAttribute("PartInserted", "true");
			    	response.sendRedirect("/car-dbs-implementation/uipages/firstPagePages/AdministratorViewLoginPage.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		// end-user-code
	}
}